package com.magicborrow;

import android.app.ActivityManager;
import android.app.Application;
import android.content.ComponentName;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.baidu.mapapi.SDKInitializer;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.magicborrow.activity.MainActivity;
import com.magicborrow.cache.BitmapLruCache;
import com.magicborrow.huanxin.DemoHXSDKHelper;
import com.magicborrow.utils.ImageLoadUtil;
import com.magicborrow.utils.UmengUtils;
import com.umeng.message.UmengRegistrar;

import cn.sharesdk.framework.ShareSDK;


/**
 * Created by River on 6/4/15.
 */
public class MagicBorrowApplication extends Application {

    public static final String TAG = MagicBorrowApplication.class
            .getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private int Tag;
    private MainActivity mainActivty;

    public static String getTAG() {
        return TAG;
    }

    public void setTag(int tag) {
        Tag = tag;
    }

    private static MagicBorrowApplication mInstance;

    public MainActivity getMainActivty() {
        return mainActivty;
    }

    public void setMainActivty(MainActivity mainActivty) {
        this.mainActivty = mainActivty;
    }

    // login user name
    public final String PREF_USERNAME = "username";

    /**
     * 当前用户nickname,为了苹果推送不是userid而是昵称
     */
    public static String currentUserNick = "";
    public static DemoHXSDKHelper hxSDKHelper = new DemoHXSDKHelper();

    /**
     * ChenRen/2013-12-30 重写当前app是否在前端运行的逻辑(之前的有bug, 见{@link })
     * @return
     */
    static public boolean isBackground()
    {
        // ChenRen/2013-12-30
        //return !MyLifecycleHandler.isApplicationInForeground();

        // ChenRen/2013-12-30
        ActivityManager am = (ActivityManager) MagicBorrowApplication.getInstance().getSystemService(ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

//		Log.d("cn.getPackageName():"+cn.getPackageName() +"; MyApplication.getInstance().getPackageName():"+MyApplication.getInstance().getPackageName());
        return !cn.getPackageName().equals(MagicBorrowApplication.getInstance().getPackageName());
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(this);
        EMChat.getInstance().init(this);
        EMChat.getInstance().setDebugMode(false);
        ShareSDK.initSDK(this);
        mInstance = this;
        ImageLoadUtil.configuration(this);
        UmengUtils.startPush(this);
        String device_token = UmengRegistrar.getRegistrationId(this);
        UmengUtils.saveDeviceToke(this,device_token);
        BaseLog.e("==device_token==",device_token);
        if(!hxSDKHelper.onInit(this)){
            Log.e("xxxxxx","xxxxxx");
            //hxSDKHelper.onInit(this);
            EMChatOptions options = EMChatManager.getInstance().getChatOptions();
        }
    }

    public static synchronized MagicBorrowApplication getInstance() {
        return mInstance;
    }


    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new BitmapLruCache(this));
        }
        return this.mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        req.setRetryPolicy(new DefaultRetryPolicy(
                5000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

    /**
     * 获取当前登陆用户名
     *
     * @return
     */
    public String getUserName() {
        return hxSDKHelper.getHXId();
    }

    /**
     * 获取密码
     *
     * @return
     */
    public String getPassword() {
        return hxSDKHelper.getPassword();
    }

    /**
     * 设置用户名
     *
     * @param
     */
    public void setUserName(String username) {
        hxSDKHelper.setHXId(username);
    }

    /**
     * 设置密码 下面的实例代码 只是demo，实际的应用中需要加password 加密后存入 preference 环信sdk
     * 内部的自动登录需要的密码，已经加密存储了
     *
     * @param pwd
     */
    public void setPassword(String pwd) {
        hxSDKHelper.setPassword(pwd);
    }

    /**
     * 退出登录,清空数据
     */
    public void logout(final boolean isGCM,final EMCallBack emCallBack) {
        // 先调用sdk logout，在清理app中自己的数据
        hxSDKHelper.logout(isGCM,emCallBack);
    }


    private boolean isPass;

    public boolean isPass() {
        return isPass;
    }

    public void setPass(boolean pass) {
        isPass = pass;
    }
}
